package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.page.PageList;
import com.ruoyi.common.core.page.PageQuery;
import com.ruoyi.common.core.page.PageUtil;
import com.ruoyi.common.core.page.ResultEntity;
import com.ruoyi.common.utils.CodeUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.TDevicePartsRecord;
import com.ruoyi.system.domain.TProd;
import com.ruoyi.system.domain.TProdTask;
import com.ruoyi.system.domain.dto.TProdTaskDTO;
import com.ruoyi.system.domain.vo.TDevicePartsRecordVO;
import com.ruoyi.system.service.ISysAdminUserService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.ITDevicePartsRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Api(tags = "(客户端) 设备维护-易损件更换记录")
@RestController
@RequestMapping("/client/device/parts")
public class TDevicePartsRecordController {
    @Autowired
    private ITDevicePartsRecordService partsRecordService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysAdminUserService adminUserService;

    @GetMapping("/list")
    @ApiOperation(value = "更换记录列表查询",response = TDevicePartsRecordVO.class)
    public ResultEntity list(@RequestParam(value = "projectId") Long projectId, PageQuery pageQuery){
        List<TDevicePartsRecord> list =  partsRecordService.listPartsRecord(projectId,pageQuery);
        list.forEach(item -> {
            if(item.getUpdateBy() > 100000L) {
                item.setUpdateByName(sysUserService.getById(item.getCreateBy()).getName());
            }else {
                item.setUpdateByName(adminUserService.getById(item.getCreateBy()).getName());
            }
        });
        return ResultEntity.createSuccess(PageUtil.getPageList(list));
    }

    @PostMapping("/saveOrUpdate")
    @ApiOperation(value = "新增/编辑更换记录")
    public ResultEntity saveOrUpdate(@RequestBody TDevicePartsRecord params) {
        partsRecordService.saveOrUpdate(params);
        return ResultEntity.createSuccess();
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除更换记录")
    public ResultEntity delete(@RequestBody TDevicePartsRecord params) {
        partsRecordService.removeById(params.getId());
        return ResultEntity.createSuccess();
    }
}
